package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.analyse.JSoupUtils;
import com.kdtech.crawler.CrawlHTML;
import com.kdtech.crawler.at.UrlArgumentTop;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.HtmlCleaner;
import com.kdtech.utils.StringUtils;
import com.kdtech.utils.HtmlCleaner;

/**
 * http://www.576tv.com/ 台州在线
 *  abc
 */
public class _576tvNewsAnalyse implements AnalyseNews{
	
	public boolean isDetailPage(String url) {
		boolean bRet=false;
		String[] regex={
				"http://www.576tv.com/Program/[0-9]*.html",
				"http://.*.blog.576tv.com/archives/[0-9]*.aspx",
				"http://blog.576tv.com/redirect.aspx?go=log&userid=19&id=11585",
				"http://.*.576tv.com/news/[a-z0-9]{16,}.html",
				"http://.*.576tv.com/album/[a-z0-9]{16,}.html",
				};
		for (int i=0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return bRet;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsMeta news=new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}
		String htmltxt=urlMeta.getHtml();
		String url=urlMeta.getUrl();
		if(!isDetailPage(url)){
		}
		news.setUrl(url);

		String title=null;
		String content=null;
		Long date=null;
		Document doc=Jsoup.parse(htmltxt);
		title =doc.select(".program_hd h1").text();
		if(StringUtils.isBlank(title)){
			title =doc.select("#userblog_topic").text();
		}
		if(StringUtils.isBlank(title)){
			title =doc.select(".row h1").text();
		}
		if(date==null){
			date= JSoupUtils.matchDate(doc, "日期:","时间:");
		}
		if(date==null){
			date=DateUtils.matchDate(doc.select(".row h4").text());
		}
		String author = JSoupUtils.matchAuthor(doc, "文章来源：","作者:","来源:","日期：");

		doc.select(".program_source").remove();
		content=HtmlCleaner.getContentHtml(url,doc.select(".program_bd"));
		if(StringUtils.isBlank(content)){
			content=HtmlCleaner.getContentHtml(url,doc.select(".logtext"));
		}
		if(StringUtils.isBlank(content)){
			content=HtmlCleaner.getContentHtml(url,doc.select(".tab-content"));
		}
		if(StringUtils.isBlank(content)){
			content=HtmlCleaner.getContentHtml(url,doc.select(".Pcontent"));
		}
		news.setAuthor(author);
		news.setTitle(StringUtils.trimSpace(title));
		news.setContent(content);
		news.setDate(date);

		return news;
	}
	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}
	public static void main(String[] args) {
		_576tvNewsAnalyse a=new _576tvNewsAnalyse();
		String url="http://mpugongying.576tv.com/news/545c72df231c0f0e94865167.html";
		String fromatUrl=UrlArgumentTop.FromatUrl(url);
		System.out.println(a.isDetailPage(fromatUrl));
		UrlMeta meta=CrawlHTML.responseToURL(fromatUrl);
		NewsMeta parserHtml=a.parserHtml(meta);
		System.out.println(parserHtml);
	}
	
	public boolean isNeedUpdate(){
		return false;
	}
}
